import urllib.request
import re 
import os
import csv

def imageCrawler(url,topath):
    #创建模拟浏览器
    headers = {"User-Agent":"Mozilla/5.0 (Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Mobile Safari/534.13"
        }
    #"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
    #"Mozilla/5.0 (Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Mobile Safari/534.13"
    
    # ＃创建请求体
    req = urllib.request.Request(url,headers=headers)
    # 创建一个请求
    response = urllib.request.urlopen(req)
    # 请求读取的数据
    HtmlStr = response.read().decode('utf-8')
    print(response.getcode())
    # 建立合适的需求的正则表达式，（价钱，付款人数，如实描述,服务态度,发货速度）
    pat = r'<strong>(.*?)</strong></span>.*?<span class="payNum">(.*?)</span>.*?<li><span class="morethan">如实描述:<b>(.*?)</b></span><span .*?<li><span class="morethan">服务态度:<b>(.*?)</b></span><span .*?<li><span class="morethan">发货速度:<b>(.*?)</b></span><span .*?'
    # pat = r'<img data-ks-lazyload="https://(.*?)" src' #image图片

    # 编译
    re_image = re.compile(pat,re.S)
    imageList = re_image.findall(HtmlStr)
    
    row = ["价钱","付款人数","如实描述","服务态度","发货速度"]
    with open(topath+'clothesInfo.csv','w',encoding='utf-8')as f:
        csv_f = csv.writer(f,lineterminator='\n')
        csv_f.writerow(row)##csv表头文字
        csv_f.writerows(imageList)##全部数据套进csv表格

    # print(imageList)
    print(len(imageList))#打印爬数据数量
    # num = 1
    # for imageUrl in imageList:#遍历数据
    #     path = os.path.join(topath,str(num)+".jpg")＃存放在本地的地址
    #     num += 1
    #     urllib.request.urlretrieve('http://'+imageUrl,filename=path)
    
    # with open(r'/home/tarena/mydata/爬虫/image/yhd.html','wb') as f:
    #     f.write(HtmlStr)
        


# 放到本地的地址
topath = r"/home/tarena/mydata/爬虫/image/"
#网页地址
url = "https://re.taobao.com/search_ou?keyword=%E5%A5%B3%E8%BF%9E%E8%A1%A3%E8%A3%99&catid=&refpid=mm_26632258_3504122_32538762&_input_charset=utf8&clk1=62e0c36ef442063abdc9188e9e2b9889&spm=a2e15.8261149.07626516005.1.171a29b4uLax6G"
imageCrawler(url,topath)




